package com.demo.delivery;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity implements FetchUsersListener {
	private ProgressDialog dialog;
	boolean firstRun = true;
	UserAdapter adapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		

		
		
			setTitle(" Select Delivery Boy");
			initView();
		

	}
	
	
	public void onDeliveredClicked(View v) {
		int position = (int) v.getTag();

		String user = adapter.getUser(position).getUserName();

		Intent intent = new Intent(this, DeliveredOrdersActivity.class);
		intent.putExtra("user",user);
		startActivity(intent);
		

	}


	public void onPendingClicked(View v) {
		int position = (int) v.getTag();

		String user = adapter.getUser(position).getUserName();

		Intent intent = new Intent(this, PendingOrdersActivity.class);
		intent.putExtra("user",user);
		startActivity(intent);
		

	}
	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

	

		Intent intent = new Intent(this, PendingOrdersActivity.class);
		intent.putExtra("user",
				((User) l.getItemAtPosition(position)).getUserName());
		startActivity(intent);

	}

	private void initView() {

		// show progress dialog
		dialog = ProgressDialog.show(this, "", "Loading...");

		String url = "http://qrcode.educroc.com/delivery_boy_zones";
		FetchUsersTask task = new FetchUsersTask(this);
		task.execute(url);

	}

	@Override
	public void onFetchComplete(List<User> data) {
		// dismiss the progress dialog
		if (dialog != null)
			dialog.dismiss();
		// create new adapter
		 adapter = new UserAdapter(this, data);
		// set the adapter to list
		setListAdapter(adapter);
	}

	@Override
	public void onFetchFailure(String msg) {
		// dismiss the progress dialog
		if (dialog != null)
			dialog.dismiss();
		// show failure message
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

}